package com.unittec.zk.provider.exam.service;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unittec.zk.provider.exam.mapper.ElExamDepartMapper;
import com.unittec.zk.provider.exam.entity.ElExamDepart;
import org.springframework.util.CollectionUtils;

@Service
public class ElExamDepartService extends ServiceImpl<ElExamDepartMapper, ElExamDepart> {

    public void saveAll(String examId, List<String> departs) {
        // 先删除
        QueryWrapper<ElExamDepart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ElExamDepart::getExamId, examId);
        this.remove(wrapper);

        // 再增加
        if(CollectionUtils.isEmpty(departs)){
            throw new BusinessException("请至少选择选择一个部门！！");
        }
        List<ElExamDepart> list = new ArrayList<>();

        for(String id: departs){
            ElExamDepart depart = new ElExamDepart();
            depart.setDepartId(id);
            depart.setExamId(examId);
            list.add(depart);
        }

        this.saveBatch(list);
    }

    public List<String> listByExam(String examId) {
        // 先删除
        QueryWrapper<ElExamDepart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ElExamDepart::getExamId, examId);
        List<ElExamDepart> list = this.list(wrapper);
        List<String> ids = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(ElExamDepart item: list){
                ids.add(item.getDepartId());
            }
        }
        return ids;
    }
}
